
local huangtaiji = General(extension, "lingling__huangtaiji", "qing", 4)
table.insert(Fk.lords, "lingling__huangtaiji")
local gulve = fk.CreateActiveSkill{
  name = "lingling__gulve",
  anim_type = "drawcard",
  prompt = function (self)
    return "#lingling__gulve:::"..self.interaction.data
  end,
  card_num = function (self)
    local n = tonumber(self.interaction.data)
    if player:getHandcardNum() > n then
      return player:getHandcardNum() - n
    else
      return 0
    end
  end,
  target_num = 0,
  interaction = function ()
    local choices = {}
    local n = #player:getCardIds("e")
    table.insert(choices, tostring(math.max(n - 1, 0)))
    table.insert(choices, tostring(n + 1))
    return UI.ComboBox { choices = choices }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    local n = tonumber(self.interaction.data)
    if player:getHandcardNum() > n then
      return #selected < player:getHandcardNum() - n and table.contains(player:getCardIds("h"), to_select) and
        not player:prohibitDiscard(to_select)
    else
      return false
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local n = tonumber(self.interaction.data)
    if #effect.cards == 0 then
      n = n - player:getHandcardNum()
      player:drawCards(n, self.name)
      if n > 3 and not player.dead then
        room:loseHp(player, 1, self.name)
      end
    else
      room:throwCard(effect.cards, self.name, player, player)
      if #effect.cards > 1 and player:isWounded() and not player.dead then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        }
      end
    end
  end
}
local lanqi = fk.CreateTriggerSkill{
  name = "lingling__lanqi",
  events = {fk.BeforeDrawCard},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      table.find(player.room.alive_players, function (p)
        if not table.contains(player:getTableMark(self.name), p.id) then
          if p == player then
            return #player:getCardIds("ej") >= data.num
          else
            return #p:getCardIds("hej") >= data.num
          end
        end
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      if not table.contains(player:getTableMark(self.name), p.id) then
        if p == player then
          return #player:getCardIds("ej") >= data.num
        else
          return #p:getCardIds("hej") >= data.num
        end
      end
    end)
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#lingling__lanqi-choose:::"..data.num, self.name, true, false)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local flag = "hej"
    if to == player then
      flag = "ej"
    end
    local cards = room:askForCardsChosen(player, to, data.num, data.num, flag, self.name, "#lingling__lanqi-prey::"..to.id..":"..data.num)
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
    if player.dead or to.dead then return end
    if room:askForSkillInvoke(to, self.name, nil, "#lingling__lanqi-invoke:"..player.id.."::"..#cards) then
      to:drawCards(#cards, self.name)
    else
      room:addTableMark(player, self.name, to.id)
    end
    return true
  end
}
Fk:loadTranslationTable{
  ["lingling__huangtaiji"] = "皇太极",
  ["#lingling__huangtaiji"] = "莽天腾苍",
  ["illustrator:lingling__huangtaiji"] = "珊瑚虫",
  ["designer:lingling__huangtaiji"] = "猪＆伶",

  ["lingling__gulve"] = "固略",
  [":lingling__gulve"] = "出牌阶段限一次，你可以将手牌调整至X张或Y张（X为你装备区牌数+1，Y为你装备区牌数-1），若你以此法弃置至少两张牌，"..
  "你回复1点体力，若你以此法获得至少四张牌，你失去1点体力。",
  ["lingling__lanqi"] = "揽气",
  [":lingling__lanqi"] = "每名角色限一次，当你摸牌时，你可以改为获得一名角色区域内等量的牌，然后其可以摸等量牌令此技能视为未对其发动过。"..
  "<br><br> <font color = '#a40000'>揽辔秋风听野歌，雄图开辟太宗多。<br>遥知王气归辽海，不战中原自倒戈。",
  ["#lingling__gulve"] = "固略：将手牌调整至%arg张，若弃置至少两张牌则回复1点体力，若摸至少四张牌则失去1点体力",
  ["#lingling__lanqi-choose"] = "揽气：你即将摸%arg张牌，是否改为获得一名角色区域内等量的牌？",
  ["#lingling__lanqi-prey"] = "揽气：获得 %dest 区域内%arg张牌",
  ["#lingling__lanqi-invoke"] = "揽气：你可以摸%arg张牌，或点“取消”令 %src 不能再对你发动“揽气”",
}

local yinzhen = General(extension, "lingling__yinzhen", "qing", 4)
local sumu = fk.CreateTriggerSkill{
  name = "lingling__sumu",
  anim_type = "control",
  events = {fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askForUseActiveSkill(player, "lingling__sumu_active", "#lingling__sumu-invoke", true)
    if success and dat then
      self.cost_data = {cards = dat.cards, choice = dat.interaction}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "lingling__sumu_name-round", self.cost_data.choice)
    room:setPlayerMark(player, "lingling__sumu_suit-round", Fk:getCardById(self.cost_data.cards[1]).suit)
    room:setPlayerMark(player, "@lingling__sumu-round",
      {Util.TranslateMapper(self.cost_data.choice), Fk:getCardById(self.cost_data.cards[1]):getSuitString(true)})
    room:recastCard(self.cost_data.cards, player, self.name)
  end,
}
local sumu_delay = fk.CreateTriggerSkill{
  name = "#lingling__sumu_delay",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target ~= player then
      if player:getMark("lingling__sumu_name-round") == data.card.trueName and not target.dead and not player:isNude() then
        return true
      end
      if player:getMark("lingling__sumu_suit-round") == data.card.suit and
        table.find(player.room.alive_players, function (p)
          return p:getHandcardNum() > player:getHandcardNum()
        end) then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    self.cost_data = nil
    if player:getMark("lingling__sumu_name-round") == data.card.trueName and not target.dead and not player:isNude() then
      local card = room:askForDiscard(player, 1, 1, true, "lingling__sumu", true, nil, "#lingling__sumu-damage::"..target.id, true)
      if #card > 0 then
        self.cost_data = {tos = {target.id}, cards = card}
        return true
      end
    end
    if player:getMark("lingling__sumu_suit-round") == data.card.suit and
      table.find(room.alive_players, function (p)
        return p:getHandcardNum() > player:getHandcardNum()
      end) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local yes = player:getMark("lingling__sumu_suit-round") == data.card.suit and
      table.find(room.alive_players, function (p)
        return p:getHandcardNum() > player:getHandcardNum()
      end)
    if self.cost_data then
      room:throwCard(self.cost_data.cards, "lingling__sumu", player, player)
      if not target.dead then
        room:damage{
          from = player,
          to = target,
          damage = 1,
          skillName = "lingling__sumu",
        }
      end
    end
    if yes and not player.dead then
      player:drawCards(1, "lingling__sumu")
    end
  end,
}
local sumu_active = fk.CreateActiveSkill{
  name = "lingling__sumu_active",
  card_num = 1,
  target_num = 0,
  interaction = function()
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeTrick then
        table.insertIfNeed(names, card.trueName)
      end
    end
    return UI.CardNameBox {choices = names}
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
}
local yanglian = fk.CreateTriggerSkill{
  name = "lingling__yanglian",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Draw and
      #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == target.id and table.contains({"ex_nihilo", "snatch"}, use.card.trueName)
      end, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lingling__yanglian-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = table.find(room.discard_pile, function (id)
      return Fk:getCardById(id).trueName == "ex_nihilo"
    end) and table.find(room.discard_pile, function (id)
      return Fk:getCardById(id).trueName == "snatch"
    end) and 1 or 2
    player:drawCards(n, self.name)
    if not player.dead and not player:isNude() then
      local card = room:askForCard(player, 1, 1, true, self.name, false, nil, "#lingling__yanglian-put")
      room:moveCards({
        ids = card,
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
      })
    end
  end,
}
Fk:loadTranslationTable{
  ["lingling__yinzhen"] = "雍正",
  ["#lingling__yinzhen"] = "朝乾夕惕",
  ["illustrator:lingling__yinzhen"] = "珊瑚虫",
  ["designer:lingling__yinzhen"] = "伶",

  ["lingling__sumu"] = "肃穆",
  [":lingling__sumu"] = "轮次开始时，你可以重铸一张牌，然后声明一种锦囊牌，本轮当一名其他角色：使用该锦囊牌后，你可以弃置一张牌"..
  "对其造成1点伤害；使用与重铸牌花色相同的牌后，若你手牌不为最多，你摸一张牌。",
  ["lingling__yanglian"] = "养廉",
  [":lingling__yanglian"] = "未使用过【无中生有】或【顺手牵羊】的角色摸牌阶段摸牌时，你可以摸一张牌并将一张牌置于牌堆顶，若弃牌堆"..
  "没有这两种牌，则摸一张牌改为摸两张牌。"..
  "<br><br> <font color = '#a40000'>朕昔在雍邸，自幸为天下第一闲人。",
  ["lingling__sumu_active"] = "肃穆",
  ["#lingling__sumu-invoke"] = "肃穆：重铸一张牌并声明一种锦囊牌，根据牌名和重铸牌花色本轮获得效果",
  ["@lingling__sumu-round"] = "肃穆",
  ["#lingling__sumu_delay"] = "肃穆",
  ["#lingling__sumu-damage"] = "肃穆：是否弃一张牌，对 %dest 造成1点伤害？",
  ["#lingling__yanglian-invoke"] = "养廉：%dest 摸牌阶段，你是否摸牌并将一张牌置于牌堆顶？",
  ["#lingling__yanglian-put"] = "养廉：请将一张牌置于牌堆顶",
}

local fukangan = General(extension, "lingling__fukangan", "qing", 4)
local zhenluan = fk.CreateTriggerSkill{
  name = "lingling__zhenluan",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.GameStart, fk.BeforeDrawCard},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.GameStart then
        return #player.room.draw_pile > 1
      elseif event == fk.BeforeDrawCard then
        return target == player and data.num > 0
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local cards = table.random(room.draw_pile, 100)
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true)
    else
      local cards = table.random(room.discard_pile, data.num + 1)
      data.num = 0
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
      end
    end
  end,
}
local caiyou = fk.CreateTriggerSkill{
  name = "lingling__caiyou",
  anim_type = "drawcard",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeTrick and not player:isNude() and
      player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 1, 1, true, self.name, true, nil, "#lingling__caiyou-invoke", true)
    if #cards > 0 then
      self.cost_data = {cards = cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data.cards, self.name, player, player)
    if not player.dead then
      Fk.skills["luoshen"]:use(event, target, player, data)
    end
  end,
}
local caiyou_obtain = fk.CreateTriggerSkill{
  name = "#lingling__caiyou_obtain",
  mute = true,
  events = {fk.FinishJudge},
  can_trigger = function(self, event, target, player, data)
    return target == player and not player.dead and data.reason == "luoshen" and data.card.color == Card.Black and
    player.room:getCardArea(data.card) == Card.Processing
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:obtainCard(player.id, data.card)
  end,
}
local gongcheng = fk.CreateTriggerSkill{
  name = "lingling__gongcheng",
  frequency = Skill.Wake,
  events = {fk.AfterDrawPileShuffle},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "-lingling__zhenluan|lingling__enchong", nil, true, false)
  end,
}
local enchong = fk.CreateTriggerSkill{
  name = "lingling__enchong",
  anim_type = "drawcard",
  events = {fk.TurnStart, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askForUseActiveSkill(player, "lingling__enchong_active", "#lingling__enchong-invoke", true)
    if success and dat then
      self.cost_data = {choice = dat.interaction}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = self.cost_data.choice
    room:sendLog({
      type = "#lingling__enchong_choice",
      from = player.id,
      arg = name,
      toast = true,
    })
    local pattern = "."
    if table.find(room.draw_pile, function (id)
      return Fk:getCardById(id).name == name
    end) or table.find(room.discard_pile, function (id)
      return Fk:getCardById(id).name == name
    end) then
      if math.random() < 0.4 then
        pattern = name
      else
        pattern = "^"..name
      end
    end
    local card = room:getCardsFromPileByRule(pattern, 1, "allPiles")
    if #card > 0 then
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, pattern == name, player.id)
    end
  end,
}
local enchong_active = fk.CreateActiveSkill{
  name = "lingling__enchong_active",
  card_num = 0,
  target_num = 0,
  interaction = function()
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if not card.is_derived then
        table.insertIfNeed(names, card.name)
      end
    end
    return UI.CardNameBox {choices = names}
  end,
  card_filter = Util.FalseFunc,
}
Fk:loadTranslationTable{
  ["lingling__fukangan"] = "福康安",
  ["#lingling__fukangan"] = "威行海内",
  ["illustrator:lingling__fukangan"] = "珊瑚虫",
  ["designer:lingling__fukangan"] = "伶",

  ["lingling__zhenluan"] = "镇乱",
  [":lingling__zhenluan"] = "游戏开始时，弃置牌堆100张牌，当你摸牌时，改为从弃牌堆随机获得X张牌（X为摸牌数+1）。",
  ["lingling__caiyou"] = "才猷",
  [":lingling__caiyou"] = "每回合限两次，当你使用锦囊牌结算后，你可以弃置一张牌发动〖洛神〗。",
  ["lingling__gongcheng"] = "功成",
  [":lingling__gongcheng"] = "觉醒技，当牌堆洗牌后，你失去〖镇乱〗，获得〖恩宠〗。",
  ["lingling__enchong"] = "恩宠",
  [":lingling__enchong"] = "回合开始时和回合结束时，你可以声明一张牌，然后你随机获得一张牌，有40%几率是你声明的牌。"..
  "<br><br> <font color = '#a40000'>异姓世臣，叨被至此，本朝第一人也。",
  ["#lingling__caiyou-invoke"] = "才猷：你可以弃置一张牌发动“洛神”",
  ["#lingling__caiyou_obtain"] = "才猷",
  ["#lingling__enchong-invoke"] = "恩宠：声明一个牌名并随机获得一张牌，有%40几率是你声明的牌",
  ["#lingling__enchong_choice"] = "%from 声明了 %arg",

  ["$lingling__gongcheng1"] = "幸此代君狩西事毕，得以垂名内外，更加眷宠。",
  ["$lingling__gongcheng2"] = "勋宠闾庙，今可延香也。",
}

local zuozongtang = General(extension, "lingling__zuozongtang", "qing", 3)
local shoufu = fk.CreateActiveSkill{
  name = "lingling__shoufu",
  anim_type = "control",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__shoufu",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and Fk:currentRoom():getPlayerById(to_select):getHandcardNum() > 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local card = room:askForCardChosen(player, target, "he", self.name, "#lingling__shoufu-prey::"..target.id)
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
    if not target.dead and not player.dead then
      room:addTableMark(player, self.name, target.id)
      room:setPlayerMark(target, "@@lingling__shoufu", 1)
    end
  end
}
local shoufu_delay = fk.CreateTriggerSkill{
  name = "#lingling__shoufu_delay",
  anim_type = "control",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if #player:getTableMark("lingling__shoufu") > 0 then
      for _, move in ipairs(data) do
        if move.to and move.toArea == Player.Hand and
          table.contains(player:getTableMark("lingling__shoufu"), move.to) then
          local p = player.room:getPlayerById(move.to)
          if p.phase ~= Player.Draw and not p.dead then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(p:getCardIds("h"), info.cardId) then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.to and move.toArea == Player.Hand and
        table.contains(player:getTableMark("lingling__shoufu"), move.to) then
        local p = room:getPlayerById(move.to)
        if p.phase ~= Player.Draw and not p.dead then
          table.insert(targets, move.to)
        end
      end
    end
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      if player.dead then return end
      local p = room:getPlayerById(id)
      if not p.dead then
        self:doCost(event, p, player, data)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, move in ipairs(data) do
      if move.to == target.id and move.toArea == Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(target:getCardIds("h"), info.cardId) then
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
    end
    if #cards > 0 then
      local card = room:askForCard(target, 1, 1, false, "lingling__shoufu", false, tostring(Exppattern{ id = cards }),
        "#lingling__shoufu-give:"..player.id)
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonGive, "lingling__shoufu", nil, false, target.id)
    end
  end,

  refresh_events = {fk.AfterTurnEnd},
  can_refresh = function(self, event, target, player, data)
    return table.contains(player:getTableMark("lingling__shoufu"), target.id)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@lingling__shoufu", 0)
    local mark = player:getTableMark("lingling__shoufu")
    for i = #mark, 1, -1 do
      if mark[i] == target.id then
        table.remove(mark, i)
      end
    end
    room:setPlayerMark(player, "lingling__shoufu", mark)
  end,
}
local zhenya = fk.CreateActiveSkill{
  name = "lingling__zhenya",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__zhenya",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and Fk:currentRoom():getPlayerById(to_select).hp > 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
    if not target.dead and not player.dead then
      room:addTableMark(player, self.name, target.id)
      room:setPlayerMark(target, "@@lingling__zhenya", 1)
    end
  end
}
local zhenya_delay = fk.CreateTriggerSkill{
  name = "#lingling__zhenya_delay",
  anim_type = "offensive",
  events = {fk.PreHpRecover},
  can_trigger = function(self, event, target, player, data)
    return table.contains(player:getTableMark("lingling__zhenya"), target.id)
  end,
  on_cost = function (self, event, target, player, data)
    self.cost_data = {tos = {target.id}}
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@lingling__zhenya", 0)
    for _, p in ipairs(room.alive_players) do
      if table.contains(p:getTableMark("lingling__zhenya"), target.id) then
        local mark = p:getTableMark("lingling__zhenya")
        for i = #mark, 1, -1 do
          if mark[i] == target.id then
            table.remove(mark, i)
          end
        end
        room:setPlayerMark(p, "lingling__zhenya", mark)
      end
    end
    return true
  end,

  refresh_events = {fk.AfterTurnEnd},
  can_refresh = function(self, event, target, player, data)
    return table.contains(player:getTableMark("lingling__zhenya"), target.id)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@lingling__zhenya", 0)
    local mark = player:getTableMark("lingling__zhenya")
    for i = #mark, 1, -1 do
      if mark[i] == target.id then
        table.remove(mark, i)
      end
    end
    room:setPlayerMark(player, "lingling__zhenya", mark)
  end,
}
local gongqin = fk.CreateTriggerSkill{
  name = "lingling__gongqin",
  anim_type = "masochism",
  frequency = Skill.Compulsory,
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      table.find(player.player_skills, function (s)
        return s.frequency == Skill.Limited
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = #table.filter(player.player_skills, function (s)
      return s.frequency == Skill.Limited
    end)
    local choices = {"lingling__gongqin_draw:::"..n}
    for _, s in ipairs(player.player_skills) do
      if s.frequency == Skill.Limited and player:usedSkillTimes(s.name, Player.HistoryGame) > 0 then
        table.insertIfNeed(choices, s.name)
      end
    end
    local choice = room:askForChoice(player, choices, self.name, "#lingling__gongqin-choice")
    if choice:startsWith("lingling__gongqin_draw") then
      player:drawCards(n, self.name)
    else
      player:setSkillUseHistory(choice, 0, Player.HistoryGame)
    end
  end,
}
Fk:loadTranslationTable{
  ["lingling__zuozongtang"] = "左宗棠",
  ["#lingling__zuozongtang"] = "天下惜公",
  ["illustrator:lingling__zuozongtang"] = "珊瑚虫",
  ["designer:lingling__zuozongtang"] = "伶",

  ["lingling__shoufu"] = "收复",
  [":lingling__shoufu"] = "限定技，出牌阶段，你可以获得一名手牌多于1的其他角色一张牌，则直到其下回合结束，当其于摸牌阶段外获得牌后须交给"..
  "你其中一张。",
  ["lingling__zhenya"] = "镇压",
  [":lingling__zhenya"] = "限定技，出牌阶段，你可以对一名体力大于1的其他角色造成1点伤害，则直到其下回合结束，当其下一次回复体力时防止之。",
  ["lingling__gongqin"] = "躬亲",
  [":lingling__gongqin"] = "当你受到伤害后，你摸X张牌（X为你的限定技数）或重置一个限定技。"..
  "<br><br> <font color = '#a40000'>为国尽忠，亦以季高为冠。国幸有左宗棠也。",
  ["#lingling__shoufu"] = "收复：获得一名角色一张牌，且其于摸牌阶段外获得牌时须交给你一张！",
  ["#lingling__shoufu-prey"] = "收复：获得 %dest 一张牌",
  ["@@lingling__shoufu"] = "收复",
  ["#lingling__shoufu_delay"] = "收复",
  ["#lingling__shoufu-give"] = "收复：你须交给 %src 其中一张牌",
  ["#lingling__zhenya"] = "镇压：对一名角色造成1点伤害，且防止其回复体力！",
  ["@@lingling__zhenya"] = "镇压",
  ["#lingling__zhenya_delay"] = "镇压",
  ["#lingling__gongqin-choice"] = "躬亲：选择摸牌，或重置一个限定技",
  ["lingling__gongqin_draw"] = "摸%arg张牌",

  ["$lingling__shoufu1"] = "所谓疆边无用，实在荒谬！",
  ["$lingling__shoufu2"] = "今不存边野，则明日之京畿为外贼所侵吞也。",
  ["$lingling__zhenya1"] = "受皇命，扫合诸部，弹压地方！",
  ["$lingling__zhenya2"] = "犬以齿牙事人，拔其牙则乖驯也。",
}

local hongxiuquan = General(extension, "lingling__hongxiuquan", "qun", 4)
local tianguo = fk.CreateTriggerSkill{
  name = "lingling__tianguo",
  anim_type = "big",
  frequency = Skill.Compulsory,
  events = {fk.GameStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, table.map(room.alive_players, Util.IdMapper))
    for _, p in ipairs(room:getAlivePlayers()) do
      if not p.dead and p.hp > 1 then
        local n = p.hp - 1
        room:loseHp(p, n, self.name)
        if not p.dead then
          room:changeShield(p, n)
        end
      end
    end
  end,
}
local tianguo_maxcards = fk.CreateMaxCardsSkill {
  name = "#lingling__tianguo_maxcards",
  frequency = Skill.Compulsory,
  correct_func = function(self, player)
    if player.shield > 0 then
      return player.shield * #table.filter(Fk:currentRoom().alive_players, function (p)
        return p:hasSkill(tianguo)
      end)
    end
  end
}
local tiandi = fk.CreateTriggerSkill{
  name = "lingling__tiandi",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished, fk.CardRespondFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      player:usedSkillTimes(self.name, Player.HistoryTurn) < 1 + player:getMark(self.name) and
      table.find(player.room.discard_pile, function (id)
        return Fk:getCardById(id).type == data.card.type
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.discard_pile, function (id)
      return Fk:getCardById(id).type == data.card.type
    end)
    room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
  end,
}
local hongyang = fk.CreateActiveSkill{
  name = "lingling__hongyang",
  anim_type = "control",
  card_num = 1,
  target_num = 0,
  prompt = "#lingling__hongyang",
  can_use = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == Card.Red and
      not player:prohibitDiscard(to_select)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:throwCard(effect.cards, self.name, player, player)
    room:moveCardTo(room:getNCards(10), Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true)
  end
}
local taiping = fk.CreateTriggerSkill{
  name = "lingling__taiping",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:getMark("@lingling__taiping") < 15 then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "peach" then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).trueName == "peach" then
            n = n + 1
          end
        end
      end
    end
    n = math.min(n, 15 - player:getMark("@lingling__taiping"))
    local yes1, yes2, yes3
    if player:getMark("@lingling__taiping") < 5 and player:getMark("@lingling__taiping") + n > 4 then
      yes1 = true
    end
    if player:getMark("@lingling__taiping") < 10 and player:getMark("@lingling__taiping") + n > 9 then
      yes2 = true
    end
    if player:getMark("@lingling__taiping") < 15 and player:getMark("@lingling__taiping") + n > 14 then
      yes3 = true
    end
    room:addPlayerMark(player, "@lingling__taiping", n)
    if yes1 and player:isWounded() then
      room:recover{
        who = player,
        num = math.min(2, player.maxHp - player.hp),
        recoverBy = player,
        skillName = self.name,
      }
      if player.dead then return end
    end
    if yes2 then
      player:drawCards(5, self.name)
      if player.dead then return end
    end
    if yes3 then
      room:setPlayerMark(player, "lingling__tiandi", 1)
      room:handleAddLoseSkills(player, "-lingling__hongyang|-lingling__taiping", nil, true, false)
    end
  end,

  on_lose = function (self, player, is_death)
    player.room:setPlayerMark(player, "@lingling__taiping", 0)
  end,
}
Fk:loadTranslationTable{
  ["lingling__hongxiuquan"] = "洪秀全",
  ["#lingling__hongxiuquan"] = "光复凶焰",
  ["illustrator:lingling__hongxiuquan"] = "龙凤",
  ["designer:lingling__hongxiuquan"] = "伶",

  ["lingling__tianguo"] = "天国",
  [":lingling__tianguo"] = "游戏开始时，所有角色各失去体力至1点，然后各获得X点护甲（X为失去的体力）。所有角色的手牌上限加其护甲值。",
  ["lingling__tiandi"] = "天弟",
  [":lingling__tiandi"] = "每回合限一次，当你使用或打出牌结算后，你随机获得弃牌堆的一张同类型牌。",
  ["lingling__hongyang"] = "红羊",
  [":lingling__hongyang"] = "每回合限两次，出牌阶段，你可以弃置一张红色牌，然后弃置牌堆顶的十张牌。",
  ["lingling__taiping"] = "太平",
  [":lingling__taiping"] = "每当一张【桃】进入弃牌堆后，你记录之，若记录数达到：5，你回复2点体力；10，你摸五张牌；15，〖天弟〗改为"..
  "“每回合限两次”，然后失去〖红羊〗和〖太平〗。"..
  "<br><br> <font color = '#a40000'>地狱沉沉二百年，忽遇天王洪秀全；<br>满人逃往热河边，曾国藩来做汉奸。<br>洪家杀尽汉家亡，依旧猢狲做帝王。",
  ["#lingling__hongyang"] = "红羊：你可以弃置一张红色牌，弃置牌堆顶的十张牌",
  ["@lingling__taiping"] = "太平",

  ["$lingling__tianguo1"] = "太皇御父上帝的福音！",
  ["$lingling__tianguo2"] = "世上尘泥烂蹉跎，父帝命旨太平国！",
}
